package com.jy.unkown.AD;

import android.app.Activity;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;

import com.google.gson.Gson;
import com.jiayou.CommonHost;
import com.jy.common.BaseApplication;
import com.jy.common.CSJUtils;
import com.jy.http.JsonCallBack;
import com.jy.unkown.AdCallBack;
import com.jy.unkown.AdReqCallBack;
import com.jy.unkown.IPUtils;
import com.jy.unkown.RomUtil;
import com.jy.unkown.UnkownAdManager;
import com.jy.unkown.UnkownTool;
import com.jy.unkown.entity.UnkownAdBean;
import com.jy.unkown.entity.UnkownAppBean;
import com.jy.unkown.entity.UnkownContents;
import com.jy.unkown.entity.UnkownDeviceBean;
import com.jy.unkown.entity.UnkownNetworkBean;
import com.jy.unkown.entity.UnkownReqBean;
import com.jy.unkown.entity.UnkownResponse;
import com.jy.unkown.entity.UnkownSlotBean;
import com.jy.unkown.http.OkGo;
import com.jy.unkown.view.UnkownADView;
import com.jy.utils.AppGlobals;
import com.jy.utils.cache.SpManager;
import com.jy.utils.cache.k;
import com.jy.utils.utils.ApkUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseUnkownAd {

    UnkownAdBean unkownAdBean;

    AdReqCallBack adReqCallBack;

    String appId;
    String adId;
    String requestId;
    /**
     * 价格(单位分) 部分预算支持竞价,需要提前商务人员约定
     */
    int price;


    AdCallBack adCallBack;

    boolean videoMute;

    public void setVideoMute(boolean videoMute) {
        this.videoMute = videoMute;
    }

    public boolean isVideoMute() {
        return videoMute;
    }

    public void setAdCallBack(AdCallBack adCallBack) {
        this.adCallBack = adCallBack;
    }

    public void setAdReqCallBack(AdReqCallBack adReqCallBack) {
        this.adReqCallBack = adReqCallBack;
    }

    public UnkownAdBean getUnkownAdBean() {
        return unkownAdBean;
    }

    public AdCallBack getAdCallBack() {
        return adCallBack;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getPrice() {
        return price;
    }

    public int getAdPrice() {
        if (unkownAdBean != null) {
            return unkownAdBean.price;
        }
        return 0;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    public String getRequestId() {
        return requestId;
    }

    public void setAdId(String adId) {
        this.adId = adId;
    }

    public String getAdId() {
        return adId;
    }


    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getAppId() {
        return appId;
    }

    UnkownReqBean getRequestBean() {
        UnkownReqBean unkownReqBean = new UnkownReqBean();
        unkownReqBean.requestId = getRequestId();
        UnkownAppBean unkownAppBean = new UnkownAppBean();
        unkownAppBean.appId = appId;
        unkownAppBean.appName = "美好天气";
        unkownAppBean.appPackage = "com.tianqi.meihao";
        unkownAppBean.appVersion = "1.8.2";
        unkownReqBean.app = unkownAppBean;

        UnkownDeviceBean unkownDeviceBean = new UnkownDeviceBean();
        JSONObject jsonObject = CSJUtils.getJSONObject();

        unkownDeviceBean.oaid = jsonObject.optString("oaid");
        unkownDeviceBean.mac = jsonObject.optString("mac");
        unkownDeviceBean.androidId = jsonObject.optString("android_id");
        if (TextUtils.isEmpty(unkownDeviceBean.oaid)) {
            unkownDeviceBean.oaid = UnkownTool.getOaid();
        }
        unkownDeviceBean.imei = jsonObject.optString("imei");

        unkownDeviceBean.osType = "1";


        boolean isHarmos = UnkownTool.isHarmonyOS() || UnkownTool.isHarmony(BaseApplication.getBaseApplication());
        if (isHarmos) {
            unkownDeviceBean.osType = "3";
            unkownDeviceBean.hmsCore = UnkownTool.hmsCore();
            unkownDeviceBean.appstoreVersion = UnkownTool.huaweiMarketVersion();
        }

        if (RomUtil.isOppo()) {
            unkownDeviceBean.appstoreVersion = UnkownTool.getOppoMarketVersion();
        } else if (RomUtil.isVivo()) {
            unkownDeviceBean.appstoreVersion = UnkownTool.getVivoMarketVersion();
        }


        unkownDeviceBean.osVersion = Build.VERSION.RELEASE;
        unkownDeviceBean.vendor = Build.PRODUCT;

        unkownDeviceBean.screenHeight = BaseApplication.getBaseApplication().getResources().getDisplayMetrics().heightPixels + "";
        unkownDeviceBean.screenWidth = BaseApplication.getBaseApplication().getResources().getDisplayMetrics().widthPixels + "";

        unkownDeviceBean.ua = WebSettings.getDefaultUserAgent(BaseApplication.getBaseApplication());

        unkownDeviceBean.screenOrientation = "1";
//        unkownDeviceBean.ppi = "" + BaseApplication.getBaseApplication().getResources().getDisplayMetrics().density;


        unkownDeviceBean.brand = Build.BRAND;
        unkownDeviceBean.model = Build.MODEL;
        unkownDeviceBean.country = "CN";
        unkownDeviceBean.deviceType = "1";

        unkownDeviceBean.appList = APLiTool.collectAppList();

        unkownDeviceBean.wx_installed = "" + ApkUtils.isInstall("com.tencent.mm");


        if (!isHarmos) {
            if (RomUtil.isMiui()) {
                unkownDeviceBean.miuiVersion = "" + UnkownTool.getMiuiVersion();
            }
        }


        unkownReqBean.device = unkownDeviceBean;


        UnkownSlotBean unkownSlotBean = new UnkownSlotBean();
        unkownSlotBean.adtype = getAdType();
        unkownSlotBean.slotId = adId;
        int w = BaseApplication.getBaseApplication().getDawuWidth();
        unkownSlotBean.slotwidth = w;
        unkownSlotBean.slotheight = 0;

        unkownSlotBean.bidPrice = "";
//        unkownSlotBean.
        unkownSlotBean = getUnkownSoltReqBean(unkownSlotBean);

        unkownReqBean.slot = unkownSlotBean;


        UnkownNetworkBean unkownNetworkBean = new UnkownNetworkBean();


        unkownNetworkBean.cellular_id = UnkownTool.cellId();
        unkownNetworkBean.ip = IPUtils.getIp();
        unkownNetworkBean.lat = Double.parseDouble(SpManager.getString(k.lat, "0.0"));
        unkownNetworkBean.lon = Double.parseDouble(SpManager.getString(k.lng, "0.0"));

        if (unkownNetworkBean.lat == 100 && unkownNetworkBean.lon == 100) {
            unkownNetworkBean.lat = unkownNetworkBean.lon = 0;
        }

        String netType = UnkownTool.getNetworkTypeStr();
        if (TextUtils.equals("Unknown", netType)) {
            unkownNetworkBean.connectionType = 0;
        } else if (TextUtils.equals("2G", netType)) {
            unkownNetworkBean.connectionType = 2;
        } else if (TextUtils.equals("3G", netType)) {
            unkownNetworkBean.connectionType = 3;
        } else if (TextUtils.equals("4G", netType)) {
            unkownNetworkBean.connectionType = 4;
        } else if (TextUtils.equals("5G", netType)) {
            unkownNetworkBean.connectionType = 5;
        } else if (TextUtils.equals("Wi-Fi", netType)) {
            unkownNetworkBean.connectionType = 100;
        }


        String op = UnkownTool.getOperator();
        if (TextUtils.equals("移动", op)) {
            unkownNetworkBean.operatorType = 1;
        } else if (TextUtils.equals("联通", op)) {
            unkownNetworkBean.operatorType = 3;
        } else if (TextUtils.equals("电信", op)) {
            unkownNetworkBean.operatorType = 2;
        } else if (TextUtils.equals("铁通", op)) {
            unkownNetworkBean.operatorType = 99;
        } else {
            unkownNetworkBean.operatorType = 0;
        }
        unkownReqBean.network = unkownNetworkBean;

        return unkownReqBean;
    }

    public abstract UnkownSlotBean getUnkownSoltReqBean(UnkownSlotBean unkownSlotBean);

    public void loadAd() {
//        if (CommonHost.isDebug()) {
//            UnkownContents.url = "http://api.bjzghd.com/adx/ssp/dspAdTest";
//        } else {
//        }

//        UnkownContents.url = "http://api.bjzghd.com/adx/ssp/dspAdVideo";
        UnkownContents.url = "http://api.bjzghd.com/adx/ssp/dspAdTest";

        UnkownReqBean unkownReqBean = getRequestBean();
        String json = new Gson().toJson(unkownReqBean);
        try {
            JSONObject jsonObject = new JSONObject(json);

            JSONObject slotJSONobject = jsonObject.optJSONObject("slot");
            JSONObject slot = new JSONObject();
            slot.put("adtype", slotJSONobject.optInt("adtype"));
            slot.put("bidPrice", slotJSONobject.optInt("bidPrice"));
            slot.put("slotId", slotJSONobject.optString("slotId"));
            slot.put("slotheight", slotJSONobject.optInt("slotheight"));
            slot.put("slotwidth", slotJSONobject.optInt("slotwidth"));
            jsonObject.put("slot", slot);


            OkGo.instance().post(UnkownContents.url, jsonObject, new JsonCallBack<UnkownResponse>() {

                @Override
                public void success(UnkownResponse unkownResponse) {
                    if (TextUtils.equals(unkownResponse.errorCode, "200") && unkownResponse.ads != null && unkownResponse.ads.size() > 0) {
                        unkownAdBean = unkownResponse.ads.get(0);
//                        String json= new Gson().toJson(unkownAdBean);
                        try {
                            UnkownAdBean.MetaGroupDTO metaGroupDTO = unkownAdBean.metaGroup.get(0);

//
//                            {
//                                List<String> strings=new ArrayList<>();
//                                strings.add("http://report.bjzghd.com/adx/report/event?param=729CE07510634CB8CF7EAB9DB54D889469AF1E07FEF3362D7995A9C96035108241FB0782ABC7B67C92EDA10B66E4150617B0C41E65E6A3712B8EF095FA35C22C8DFC4F8B1108033A&ext=D9CD8830A199CB73205AEA7D4BB43FD10EEB854ACE575FEE&event=width:__WIDTH__;height:__HEIGHT__;dx:__DOWN_X__;dy:__DOWN_Y__;ux:__UP_X__;uy:__UP_Y__;ts:__T_TS__;ms:__EVENT_TIME_START__;dpw:__DP_WIDTH__;dph:__DP_HEIGHT__;dpdx:__DP_DOWN_X__;dpdy:__DP_DOWN_Y__;dpux:__DP_UP_X__;dpuy:__DP_UP_Y__;sld:__SLD__");
//                                metaGroupDTO.winInstallUrls=strings;
//                            }
//                           {
//                                List<String> strings=new ArrayList<>();
//                                strings.add("http://report.bjzghd.com/adx/report/event?param=729CE07510634CB8CF7EAB9DB54D889469AF1E07FEF3362D7995A9C96035108241FB0782ABC7B67C92EDA10B66E4150617B0C41E65E6A3712B8EF095FA35C22C8DFC4F8B1108033A&ext=D9CD8830A199CB73205AEA7D4BB43FD10EEB854ACE575FEE&event=width:__WIDTH__;height:__HEIGHT__;dx:__DOWN_X__;dy:__DOWN_Y__;ux:__UP_X__;uy:__UP_Y__;ts:__T_TS__;ms:__EVENT_TIME_START__;dpw:__DP_WIDTH__;dph:__DP_HEIGHT__;dpdx:__DP_DOWN_X__;dpdy:__DP_DOWN_Y__;dpux:__DP_UP_X__;dpuy:__DP_UP_Y__;sld:__SLD__");
//                                metaGroupDTO.winInstallEndUrls=strings;
//                            }
//


                            if (metaGroupDTO.creativeType == 5) {
//                                metaGroupDTO.videoUrl="https://f.video.weibocdn.com/o0/21AHWKy9lx08aJ7hFP0Q01041200hpL80E010.mp4?label=mp4_720p&template=1024x576.25.0&media_id=4975904213106715&tp=8x8A3El:YTkl0eM8&us=0&ori=1&bf=4&ot=h&ps=3lckmu&uid=3ZoTIp&ab=,8012-g2,8143-g0,8013-g0,3601-g38&Expires=1701950196&ssig=zjPl683uw2&KID=unistore,video";
                                String url = UnkownAdManager.getProxy().getProxyUrl(metaGroupDTO.videoUrl);
                                metaGroupDTO.videoUrl = url;
                            }
                        } catch (Exception exception) {

                        }


                        adReqCallBack.reqSucc();
                    } else {
                        adReqCallBack.reqErr();
                    }
                }

                @Override
                public void error(Exception e) {
                    Log.wtf("TAG", e);
                    adReqCallBack.reqErr();
                }
            });

        } catch (Exception exception) {
            adReqCallBack.reqErr();
        }
    }


    abstract int getAdType();

    public abstract UnkownADView getADView(Activity activity) throws Exception;


}
